﻿using System.Collections.Generic;
using System.Windows.Documents;

namespace VSTalk.Engine.ViewModel.Messenger.ThreadRenderer.Model
{
    public class ConversationModel
    {
        private FlowDocument Document { get; set; }


        private IList<ConversationSection> _sections = new List<ConversationSection>();

        public IList<ConversationSection> Sections
        {
            get { return _sections; }
        }

        public ConversationModel(FlowDocument document)
        {
            Document = document;
        }

        public void AddFirst(ConversationSection section)
        {
            if (Document.Blocks.Count == 0)
            {
                _sections.Add(section);
                Document.Blocks.Add(section.Content);
            }
            else
            {
                _sections.Insert(0, section);
                var first = Document.Blocks.FirstBlock;
                Document.Blocks.InsertBefore(first, section.Content);
            }
        }

        public void AddLast(ConversationSection section)
        {
            _sections.Add(section);
            Document.Blocks.Add(section.Content);
        }

        public void AddAfter(ConversationSection section, ConversationSection point)
        {
            var index = _sections.IndexOf(point);
            _sections.Insert(index + 1, section);
            Document.Blocks.InsertAfter(point.Content, section.Content);
        }

        public void AddBefore(ConversationSection section, ConversationSection point)
        {
            var index = _sections.IndexOf(point);
            _sections.Insert(index, section);
            Document.Blocks.InsertBefore(point.Content, section.Content);
        }

        public void Remove(ConversationSection targetSection)
        {
            _sections.Remove(targetSection);
            Document.Blocks.Remove(targetSection.Content);
        }
    }
}